#!/usr/bin/env python3
import numpy as np

samples_and = [
    [0, 0, 0],
    [1, 0, 0],
    [0, 1, 0],
    [1, 1, 1],
]


samples_or = [
    [0, 0, 0],
    [1, 0, 1],
    [0, 1, 1],
    [1, 1, 1],
]


samples_xor = [
    [0, 0, 0],
    [1, 0, 1],
    [0, 1, 1],
    [1, 1, 0],
]



def perceptron(samples):
    w = np.array([1, 2])
    b = 0
    a = 1

    for i in range(10):
        for j in range(4):
            x = np.array(samples[j][:2])
            y = 1 if np.dot(w, x) + b > 0 else 0#dot求内积
            d = np.array(samples[j][2])

            delta_b = a*(d-y)
            delta_w = a*(d-y)*x

            print('epoch {} sample {}  [{} {} {} {} {} {} {}]'.format(
                i, j, w[0], w[1], b, y, delta_w[0], delta_w[1], delta_b
            ))
            w = w + delta_w
            b = b + delta_b


if __name__ == '__main__':
    print('logical and')
    perceptron(samples_and)
    print('logical or')
    perceptron(samples_or)
    print('logical xor')
    perceptron(samples_xor)
'''
运行结果如下：
logical and
epoch 0 sample 0  [1 2 0 0 0 0 0]
epoch 0 sample 1  [1 2 0 1 -1 0 -1]
epoch 0 sample 2  [0 2 -1 1 0 -1 -1]
epoch 0 sample 3  [0 1 -2 0 1 1 1]
epoch 1 sample 0  [1 2 -1 0 0 0 0]
epoch 1 sample 1  [1 2 -1 0 0 0 0]
epoch 1 sample 2  [1 2 -1 1 0 -1 -1]
epoch 1 sample 3  [1 1 -2 0 1 1 1]
epoch 2 sample 0  [2 2 -1 0 0 0 0]
epoch 2 sample 1  [2 2 -1 1 -1 0 -1]
epoch 2 sample 2  [1 2 -2 0 0 0 0]
epoch 2 sample 3  [1 2 -2 1 0 0 0]
epoch 3 sample 0  [1 2 -2 0 0 0 0]
epoch 3 sample 1  [1 2 -2 0 0 0 0]
epoch 3 sample 2  [1 2 -2 0 0 0 0]
epoch 3 sample 3  [1 2 -2 1 0 0 0]
epoch 4 sample 0  [1 2 -2 0 0 0 0]
epoch 4 sample 1  [1 2 -2 0 0 0 0]
epoch 4 sample 2  [1 2 -2 0 0 0 0]
epoch 4 sample 3  [1 2 -2 1 0 0 0]
epoch 5 sample 0  [1 2 -2 0 0 0 0]
epoch 5 sample 1  [1 2 -2 0 0 0 0]
epoch 5 sample 2  [1 2 -2 0 0 0 0]
epoch 5 sample 3  [1 2 -2 1 0 0 0]
epoch 6 sample 0  [1 2 -2 0 0 0 0]
epoch 6 sample 1  [1 2 -2 0 0 0 0]
epoch 6 sample 2  [1 2 -2 0 0 0 0]
epoch 6 sample 3  [1 2 -2 1 0 0 0]
epoch 7 sample 0  [1 2 -2 0 0 0 0]
epoch 7 sample 1  [1 2 -2 0 0 0 0]
epoch 7 sample 2  [1 2 -2 0 0 0 0]
epoch 7 sample 3  [1 2 -2 1 0 0 0]
epoch 8 sample 0  [1 2 -2 0 0 0 0]
epoch 8 sample 1  [1 2 -2 0 0 0 0]
epoch 8 sample 2  [1 2 -2 0 0 0 0]
epoch 8 sample 3  [1 2 -2 1 0 0 0]
epoch 9 sample 0  [1 2 -2 0 0 0 0]
epoch 9 sample 1  [1 2 -2 0 0 0 0]
epoch 9 sample 2  [1 2 -2 0 0 0 0]
epoch 9 sample 3  [1 2 -2 1 0 0 0]
logical or
epoch 0 sample 0  [1 2 0 0 0 0 0]
epoch 0 sample 1  [1 2 0 1 0 0 0]
epoch 0 sample 2  [1 2 0 1 0 0 0]
epoch 0 sample 3  [1 2 0 1 0 0 0]
epoch 1 sample 0  [1 2 0 0 0 0 0]
epoch 1 sample 1  [1 2 0 1 0 0 0]
epoch 1 sample 2  [1 2 0 1 0 0 0]
epoch 1 sample 3  [1 2 0 1 0 0 0]
epoch 2 sample 0  [1 2 0 0 0 0 0]
epoch 2 sample 1  [1 2 0 1 0 0 0]
epoch 2 sample 2  [1 2 0 1 0 0 0]
epoch 2 sample 3  [1 2 0 1 0 0 0]
epoch 3 sample 0  [1 2 0 0 0 0 0]
epoch 3 sample 1  [1 2 0 1 0 0 0]
epoch 3 sample 2  [1 2 0 1 0 0 0]
epoch 3 sample 3  [1 2 0 1 0 0 0]
epoch 4 sample 0  [1 2 0 0 0 0 0]
epoch 4 sample 1  [1 2 0 1 0 0 0]
epoch 4 sample 2  [1 2 0 1 0 0 0]
epoch 4 sample 3  [1 2 0 1 0 0 0]
epoch 5 sample 0  [1 2 0 0 0 0 0]
epoch 5 sample 1  [1 2 0 1 0 0 0]
epoch 5 sample 2  [1 2 0 1 0 0 0]
epoch 5 sample 3  [1 2 0 1 0 0 0]
epoch 6 sample 0  [1 2 0 0 0 0 0]
epoch 6 sample 1  [1 2 0 1 0 0 0]
epoch 6 sample 2  [1 2 0 1 0 0 0]
epoch 6 sample 3  [1 2 0 1 0 0 0]
epoch 7 sample 0  [1 2 0 0 0 0 0]
epoch 7 sample 1  [1 2 0 1 0 0 0]
epoch 7 sample 2  [1 2 0 1 0 0 0]
epoch 7 sample 3  [1 2 0 1 0 0 0]
epoch 8 sample 0  [1 2 0 0 0 0 0]
epoch 8 sample 1  [1 2 0 1 0 0 0]
epoch 8 sample 2  [1 2 0 1 0 0 0]
epoch 8 sample 3  [1 2 0 1 0 0 0]
epoch 9 sample 0  [1 2 0 0 0 0 0]
epoch 9 sample 1  [1 2 0 1 0 0 0]
epoch 9 sample 2  [1 2 0 1 0 0 0]
epoch 9 sample 3  [1 2 0 1 0 0 0]
logical xor
epoch 0 sample 0  [1 2 0 0 0 0 0]
epoch 0 sample 1  [1 2 0 1 0 0 0]
epoch 0 sample 2  [1 2 0 1 0 0 0]
epoch 0 sample 3  [1 2 0 1 -1 -1 -1]
epoch 1 sample 0  [0 1 -1 0 0 0 0]
epoch 1 sample 1  [0 1 -1 0 1 0 1]
epoch 1 sample 2  [1 1 0 1 0 0 0]
epoch 1 sample 3  [1 1 0 1 -1 -1 -1]
epoch 2 sample 0  [0 0 -1 0 0 0 0]
epoch 2 sample 1  [0 0 -1 0 1 0 1]
epoch 2 sample 2  [1 0 0 0 0 1 1]
epoch 2 sample 3  [1 1 1 1 -1 -1 -1]
epoch 3 sample 0  [0 0 0 0 0 0 0]
epoch 3 sample 1  [0 0 0 0 1 0 1]
epoch 3 sample 2  [1 0 1 1 0 0 0]
epoch 3 sample 3  [1 0 1 1 -1 -1 -1]
epoch 4 sample 0  [0 -1 0 0 0 0 0]
epoch 4 sample 1  [0 -1 0 0 1 0 1]
epoch 4 sample 2  [1 -1 1 0 0 1 1]
epoch 4 sample 3  [1 0 2 1 -1 -1 -1]
epoch 5 sample 0  [0 -1 1 1 0 0 -1]
epoch 5 sample 1  [0 -1 0 0 1 0 1]
epoch 5 sample 2  [1 -1 1 0 0 1 1]
epoch 5 sample 3  [1 0 2 1 -1 -1 -1]
epoch 6 sample 0  [0 -1 1 1 0 0 -1]
epoch 6 sample 1  [0 -1 0 0 1 0 1]
epoch 6 sample 2  [1 -1 1 0 0 1 1]
epoch 6 sample 3  [1 0 2 1 -1 -1 -1]
epoch 7 sample 0  [0 -1 1 1 0 0 -1]
epoch 7 sample 1  [0 -1 0 0 1 0 1]
epoch 7 sample 2  [1 -1 1 0 0 1 1]
epoch 7 sample 3  [1 0 2 1 -1 -1 -1]
epoch 8 sample 0  [0 -1 1 1 0 0 -1]
epoch 8 sample 1  [0 -1 0 0 1 0 1]
epoch 8 sample 2  [1 -1 1 0 0 1 1]
epoch 8 sample 3  [1 0 2 1 -1 -1 -1]
epoch 9 sample 0  [0 -1 1 1 0 0 -1]
epoch 9 sample 1  [0 -1 0 0 1 0 1]
epoch 9 sample 2  [1 -1 1 0 0 1 1]
epoch 9 sample 3  [1 0 2 1 -1 -1 -1]
分析：
感知器对异或问题训练时不能够得到收敛的模型，因为异或输出的结果在坐标轴上是交叉的两条线，没有任何一条线可以完全将这
两类结果完全分离开，所以感知机无法创建有效的线性模型解决异或问题。
'''